<?php
/*
 * 胰岛素信息反馈接口
 * */
class Apiv1_InsulinController extends Ctrl_Apiatk{
    //编辑    
    public function editAction(){
        $p = $_REQUEST;
        $pId = empty($p['sid'])?Tool_Fnc::ajaxMsg('老人ID不能为空'):Tool_Fnc::safe_string($p['sid']);
        $pAt = empty($p['at'])?Tool_Fnc::ajaxMsg('时间点不能为空'):Tool_Fnc::safe_string($p['at']);
        $pNum = empty($p['num'])?Tool_Fnc::ajaxMsg('剂量不能为空'):Tool_Fnc::safe_string($p['num']);

        $tSMO = new SeniorModel;
        $tId = $tSMO->field('id')->where('id = '.$pId)->fRow()['id'];
        if(empty($tId)){Tool_Fnc::ajaxMsg('该老人不存在');}

        $tIMO = new InsulinModel;
        $tTime = time();

        $tData = array(
            'created' => $tTime,
            'sid' => $pId,
            'date' => date('Y-m-d',$tTime),
            'week' => $this->getTimeWeek($tTime),
            'at' => $pAt,
            'dosis' => $pNum,
        );
        if(!$tIMO->insert($tData)){
            Tool_Fnc::ajaxMsg('编辑失败 001');
        }
        Tool_Fnc::ajaxMsg('编辑成功',1);
    }
    //获取周几
    private function getTimeWeek($time, $i = 0) {
        $weekarray = array('1', '2', '3', "4", "5", "6", "7");
        $oneD = 24 * 60 * 60;
        return $weekarray[date("w", $time + $oneD * ($i-1))];
    }
}
?>
